/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.dao;

import static cn.org.rapid_framework.util.ObjectUtils.isNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;

import com.tech15.task.model.Zdxzdc;
import com.tech15.task.model.ZdxzdcCljl;
import com.tech15.task.vo.query.ZdxzdcQuery;

@Repository
public class ZdxzdcDao extends BaseHibernateDao<Zdxzdc,java.lang.String>{

	public Class getEntityClass() {
		return Zdxzdc.class;
	}
	
	public Page findPage(ZdxzdcQuery query) {
		String sql = "select t.* from zdxzdc t where 1=1 ";
			if(isNotEmpty(query.getLdcx())){
				if(query.getLdcx() == 1){
					sql += " and t.zxzt <> -1 and t.zxzt <> 2 ";
				}
			}
			sql += "/~ and to_char(t.rq,'yyyy-mm-dd') = '[rqBegin]' ~/"
			+ "/~ and t.xggz like '%[xggz]%' ~/"
			+ "/~ and t.jtyq like '%[jtyq]%' ~/"
			+ "/~ and t.qcr = '[qcr]' ~/"
			+ "/~ and t.qcsj = '[qcsj]' ~/"
			+ "/~ and t.spr = '[shr]' ~/"
			+ "/~ and t.spsj = '[shsj]' ~/"
			+ "/~ and t.fgld = '[fgld]' ~/"
			+ "/~ and t.zxr = '[zxr]' ~/"
			+ "/~ and t.wcqk = '[wcqk]' ~/"
			+ "/~ and t.spzt = '[spzt]' ~/"
			+ "/~ and t.zxzt = '[zxzt]' ~/"
			+ "/~ order by [sortColumns] ~/";
		
		Map map = new HashMap();
        map.put("id", "String");
        map.put("rq", "Timestamp");
        map.put("xggz", "String");
        map.put("jtyq", "String");
        map.put("fgldxm", "String");
        map.put("wcqk", "String");
        map.put("fgldxm", "String");
        map.put("zxrxm", "String");
        map.put("spzt", "Integer");
        map.put("zxzt", "Integer");
        
		//return pageQuery(sql2.toString(),query);
        return pageSqlQuery(sql, query, map, Zdxzdc.class);
	}

	public Page myTask(ZdxzdcQuery query) {
		StringBuilder sql2 = new StringBuilder();
		sql2.append("select t.* from (");
		sql2.append("select t.*,'' as jlid from zdxzdc t where 1=1 ");
		if(isNotEmpty(query.getXggz())) {
            sql2.append(" and  t.xggz like '%"+query.getXggz()+"%' ");
        }
        if(isNotEmpty(query.getJtyq())) {
        	sql2.append(" and  t.jtyq like '%"+query.getJtyq()+"%' ");
        }
        
        if(query.getZtbs() == 0){
	        if(isNotEmpty(query.getUserid())) {
	        	sql2.append(" and (  ");
	        	sql2.append(" 	(t.spr = "+query.getUserid()+" and t.spzt=0) ");
	        	sql2.append(" 	or (t.fgld = "+query.getUserid()+" and t.spzt=1 and (t.zxzt=-1 or t.zxzt=3)) ");
	        	//在部门领导审核时，把分配下的多个执行任务，只显示督查基本信息
	        	sql2.append(" 	or (select count(*) from zdxzdc_cljl a where a.dcid=t.id and a.zt=3 and t.fgld="+query.getUserid()+")>0 ");
	        	sql2.append(" )  ");
	        }
	        
	        sql2.append(" union ");
	        sql2.append(" select t.*, t1.id as jlid from zdxzdc_cljl t1 left join zdxzdc t on t1.dcid=t.id where ");
	        sql2.append(" ( ");
	        sql2.append(" ((t1.zt=0 or t1.zt=2) and t1.zxr="+query.getUserid()+") )");
//	      这个查询条件是判断待审核的任务
	        //or (t1.zt=3 and t.fgld="+query.getUserid()+") 
	        sql2.append(" ) t where 1=1  order by t.qcsj desc ");
	        
        }else{
        	if(isNotEmpty(query.getUserid())) {
	        	sql2.append(" and (t.spr = "+query.getUserid()+" or t.fgld = "+query.getUserid()+" or t.zxr = "+query.getUserid()+")");
	        }
        	
        	sql2.append(" or (select count(*) from zdxzdc_cljl t1 where t1.dcid=t.id and t1.zxr="+query.getUserid()+")>0 ");
        	sql2.append(" ) t where 1=1   order by t.qcsj desc ");
        	
        	/*sql2.append(" union ");
            sql2.append(" select t.*, t1.id as jlid from zdxzdc_cljl t1 left join zdxzdc t on t1.dcid=t.id where t1.zt=1  and t1.zxr="+query.getUserid());
            sql2.append(" ) t where 1=1  ");*/
        }
        
        
        
        Map map = new HashMap();
        map.put("id", "String");
        map.put("rq", "Timestamp");
        map.put("xggz", "String");
        map.put("jtyq", "String");
        map.put("fgldxm", "String");
        map.put("wcqk", "String");
        map.put("fgldxm", "String");
        map.put("zxrxm", "String");
        map.put("spzt", "Integer");
        map.put("zxzt", "Integer");
        map.put("jlid", "String");
        
		//return pageQuery(sql2.toString(),query);
        return pageSqlQuery(sql2.toString(), query, map, Zdxzdc.class);
	}

	public List<Zdxzdc> searchExport(ZdxzdcQuery query) {
		String sql = "select t from Zdxzdc t where 1=1 ";
		if(isNotEmpty(query.getRqBegin())){
			sql += " and t.rq >= date '"+query.getRqBegin()+"'";
		}
		if(isNotEmpty(query.getRqEnd())){
			sql += " and t.rq <= to_date('"+query.getRqEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		if(isNotEmpty(query.getXggz())){
			sql += " and t.xggz like '%'"+query.getXggz()+"'%'";
		}
		if(isNotEmpty(query.getXggz())){
			sql += " and t.jtyq like '%'"+query.getJtyq()+"'%'";
		}
		
		return findHqlAll(sql, query);
	}

	public Integer myTaskCount(Long userid) {
		StringBuilder sql2 = new StringBuilder("");
		
		sql2.append("select sum(t.zs) from ( ");
		sql2.append("	select count(*) zs from zdxzdc t where 1=1 ");
		sql2.append(" 		and ((t.spr="+userid+" and t.spzt=0) or (t.fgld = "+userid+" and t.spzt=1 and t.zxzt=-1) )");
		sql2.append(" 	union ");
		sql2.append(" 	select count(*) as zs from zdxzdc_cljl t1 left join zdxzdc t on t1.dcid=t.id where  ( ");
		sql2.append(" 		((t1.zt=0 or t1.zt=2) and t1.zxr="+userid+") or (t1.zt=3 and t.fgld="+userid+") ");
		sql2.append(" 	) ");
		sql2.append(" )  t");
		return getCount(sql2.toString());
	}

	public List<ZdxzdcCljl> searchJlById(String id) {
		String hql = "from ZdxzdcCljl t where t.dcid=? order by t.rwsj asc ";
		return getHibernateTemplate().find(hql,id);
	}

	public List<ZdxzdcCljl> searchWcjlById(String id) {
		String hql = "from ZdxzdcCljl t where t.dcid=? and t.zt=1 order by t.rwsj asc ";
		return getHibernateTemplate().find(hql,id);
	}
	

}
